(function(window){
	function Lyric(path){
		return new Lyric.prototype.init(path);
	}
	Lyric.prototype = {
		constructior: Lyric,
		init: function(path){
            this.path=path;
        },
        times:[],
		lyrics:[],
		lyricsRow:[],
		index:-1,
		more:0,
		json:{},
		loadLyric:function(callBack){
			var $this=this;
			$.ajax({
				url:$this.path,
				dateType:"text",
				success: function(data){
					$this.parseLyric(data);
					callBack();
					// 初始化歌词行
					$this.lyricsRow = [];
					$this.loadLyricsRow();
				},
				error: function(e){
					console.log(e);
				}
			});
		},
		parseLyric:function(data){
			
			var $this = this;
			// 清空
			$this.times=[];
			$this.lyrics=[];
			var array =data.split("\n");
			//[00:00.32]
			var timeReg = /\[(\d*:\d*\.\d*)\]/
			
			// 遍历取出每条歌词
			$.each(array,function(index,ele){

				// 处理歌词
				var lrc = ele.split("]")[1];
				
				// 排除空字符串
				if(lrc==null||lrc.length==0){return true};
				$this.lyrics.push(lrc);
				

				
				
				var res = timeReg.exec(ele);
				 //console.log(res);
				if(res==null) return true;
				var timeStr = res[1]; // 00:00.32
				var res2 = timeStr.split(":");
				var min = parseInt(res2[0])*60;
				var sec = parseFloat(res2[1]);
				var time = parseFloat(Number(min+sec).toFixed(2)) ;
			    $this.times.push(time);
               
			});
	
		},
		
		currentIndex:function(currentTime){	
			var index = -1;
			var realIndex = -1;
			var oldTimes = this.times.filter((item)=>(item<=currentTime));
			this.lyricsRow.filter((item,i)=>{if(i<=oldTimes.length - 1){
				index += item;realIndex+=1;
			}});
			
            this.json={index:index,realIndex:realIndex};
            return this.json;
		},

		loadLyricsRow:function(){
			var that = this;
			$('.lycList span').map(function(){
				var row = Math.ceil($(this).height()/30);
				that.lyricsRow.push(row);
			})		
		},




	}
	Lyric.prototype.init.prototype = Lyric.prototype;
	window.Lyric = Lyric;
})(window);
